package edu.stonybrook.cs.gars.server.data;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import edu.stonybrook.cs.gars.server.data.exceptions.MapFormatException;
import edu.stonybrook.cs.gars.server.data.exceptions.UploadAYException;
import edu.stonybrook.cs.gars.server.data.exceptions.UploadException;
import edu.stonybrook.cs.gars.server.data.exceptions.UploadOTSException;
import edu.stonybrook.cs.gars.shared.users.LoggedInUser;
import edu.stonybrook.cs.gars.shared.users.User;
/**
 * This class will access the database(s) and handle the AY and OTS data This is
 * a singleton class and can not be instantiated
 * 
 * @author Tony Arra & Peter Iannone
 * 
 */
public class DataHandler {
	private static final File ayMap = new File("ay-map.txt");
	private static final File otsMap = new File("ots-map.txt");
	private static final boolean debug = true;
	private Rules ayRules;
	private Rules otsRules;
	private Hashtable<String, LoggedInUser> loggedInUsers = new Hashtable<String, LoggedInUser>();

	/**
	 * Retrieves single instance of DataHandler
	 * 
	 * @return instance of DataHandler
	 */
	public static DataHandler getInstance() {
		return DataHandlerHolder.instance;
	}

	/**
	 * Uploads an AY file to GARS database
	 * 
	 * @param ayFile
	 * @return true on success; false on failure
	 * @throws UploadAYException
	 */
	public void uploadAY(File ayFile) throws UploadAYException {
		try {
			Data data = new Data(ayFile);
			data.verify(ayRules);
			mapAY();
		} catch (UploadException e) {
			throw new UploadAYException(e.getMessage());
		}
	}

	/**
	 * Uploads an OTS file to GARS database
	 * 
	 * @param otsFile
	 * @return true on success; false on failure
	 * @throws UploadOTSException
	 */
	public void uploadOTS(File otsFile) throws UploadOTSException {
		try {
			Data data = new Data(otsFile);
			data.verify(otsRules);
			mapOTS();
		} catch (UploadException e) {
			throw new UploadOTSException(e.getMessage());
		}

	}


	/**
	 * Map the data to the database
	 */
	private void mapOTS() {

	}

	private void mapAY() {

	}

	/**
	 * Constructs single instance of DataHandler
	 */
	private DataHandler() {
		try {
			ayRules = new Rules(ayMap);
			otsRules = new Rules(otsMap);
		} catch (MapFormatException e) {
			//TODO This should halt execution of the program
			System.out.println(e.getMessage());
		} catch (IOException e) {
			//TODO This should halt execution of the program
			System.out.println(e.getMessage());
		}
	}

	/**
	 * Holds single instance of DataHandler
	 */
	private static class DataHandlerHolder {
		public static final DataHandler instance = new DataHandler();
	}

	/**
	 * Testing
	 */
	public static void main(String[] args) {
		if (debug) {
			DataHandler dataHandler = DataHandler.getInstance();
			//Upload AY file
			try {
				dataHandler.uploadAY(new File("ay.txt"));
			} catch (UploadAYException e) {
				System.out.println(e.getMessage());
			}
			//Upload OTS file
			try {
				dataHandler.uploadOTS(new File("ots.txt"));
			} catch (UploadOTSException e) {
				System.out.println(e.getMessage());
			}
			//
			
		}
	}
	
	

	public boolean isLoggedIn(String sessionID) 
	{
		return !(this.loggedInUsers.get(sessionID) == null);

	}

	public void addLoggedInUser(LoggedInUser loggedInUser) 
	{
		this.loggedInUsers.put(loggedInUser.getSessionID(), loggedInUser);

	}
	
	public void userLogout(String sessionID) 
	{
		this.loggedInUsers.remove(sessionID);

	}
}
